package com.dk.controller;


import com.dk.data.PageBean;
import com.dk.data.Result;
import com.dk.em.ConstantEnum;
import com.dk.entity.TRepayment;
import com.dk.entity.TStaging;
import com.dk.mapper.TRepaymentMapper;
import com.dk.service.ITRepaymentService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2021-11-02
 */
@RestController
@RequestMapping("/repayment")
public class TRepaymentController {

    @Autowired
    private TRepaymentMapper repaymentMapper;

    @Autowired
    private ITRepaymentService repaymentService;
    //    后台分期管理增加
    @ApiOperation(value = "后台分期管理增加功能")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "stagingId", value = "分期表id", required = true, paramType = "query"),
            @ApiImplicitParam(name = "adminId", value = "用户id", required = true, paramType = "query"),
            @ApiImplicitParam(name = "carId", value = "车id", required = true, paramType = "query"),
            @ApiImplicitParam(name = "manyMonth", value = "应还款期数(贷款几期,还多少月)", required = true, paramType = "query"),
            @ApiImplicitParam(name = "status", value = "1为已还,2为未还,3为未还清", required = true, paramType = "query"),
            @ApiImplicitParam(name = "startingTime", value = "还款开始时间", required = true, paramType = "query"),
                @ApiImplicitParam(name = "endTime", value = "还款结束时间", required = true, paramType = "query")
    })
    @PostMapping("add")
    public Result add(TRepayment repayment) {
        repayment.setDeleted(0);
        repayment.setVersion(1);
        repayment.setCreateTime(new Date());
        repayment.setUpdateTime(new Date());
        int i = repaymentMapper.insert(repayment);
        if (i > 0) {
            return Result.bulid(ConstantEnum.SUCCESS).setResult("用户分期增加成功");
        }
        return Result.bulid(ConstantEnum.ERROR).setResult("用户分期增加失败");
    }

    @ApiOperation(value = "后台分期管理修改功能")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键Id", required = true, paramType = "query"),
            @ApiImplicitParam(name = "stagingId", value = "分期表id", required = true, paramType = "query"),
            @ApiImplicitParam(name = "adminId", value = "用户id", required = true, paramType = "query"),
            @ApiImplicitParam(name = "carId", value = "车id", required = true, paramType = "query"),
            @ApiImplicitParam(name = "manyMonth", value = "应还款期数(贷款几期,还多少月)", required = true, paramType = "query"),
            @ApiImplicitParam(name = "status", value = "1为已还,2为未还,3为未还清", required = true, paramType = "query"),
            @ApiImplicitParam(name = "startingTime", value = "还款开始时间", required = true, paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "还款结束时间", required = true, paramType = "query")
    })
    @PostMapping("update")
    public Result update(TRepayment repayment) {
        repayment.setUpdateTime(new Date());
        int i = repaymentMapper.updateById(repayment);
        if (i > 0) {
            return Result.bulid(ConstantEnum.SUCCESS).setResult("用户分期修改成功");
        }
        return Result.bulid(ConstantEnum.ERROR).setResult("用户分期修改失败");
    }



    @ApiOperation(value = "后台分期管理删除功能")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键Id", required = true, paramType = "query")
    })
    @PostMapping("deleteById")
    public Result deleteById(Long id) {
        int i1 = repaymentMapper.deleteById(id);
        if (i1 > 0) {
            return Result.bulid(ConstantEnum.SUCCESS).setResult("用户分期删除成功");
        }
        return Result.bulid(ConstantEnum.ERROR).setResult("用户分期删除失败");
    }

    @ApiOperation(value = "查询后台用户分期表")
    @ApiImplicitParams({
            @ApiImplicitParam(name="num",value = "从第几条开始",required = true,paramType = "query"),
            @ApiImplicitParam(name="size",value = "查询多少条",required = true,paramType = "query")
    })
    @GetMapping("findAllByRepayment")
    public Result findAllByRepayment(Integer num,Integer size){
        PageBean pageBean=repaymentService.findAllByRepayment(num,size);
        return Result.bulid(ConstantEnum.SUCCESS).setResult(pageBean);
    }
}
